package com.hdaccp.gymxmjpa.Service;

import com.hdaccp.gymxmjpa.Dao.MenberDao;
import com.hdaccp.gymxmjpa.entity.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MenberDaoImpl {


    @Autowired
    private MenberDao menberDao;

    @PersistenceContext
    private EntityManager entityManager;

    public Map<String,Object> query(Map<String,Object> map1){
        //根据会员办卡的日期修改到期后状态
        List<Member> memberslist=menberDao.findAll();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String zifu = sdf.format(d);
        for (Member list:memberslist){

            String data= list.getNenberDate().toString();
            if(data.equals(zifu)){
                Member m= menberDao.findById(list.getMemberId()).get();
                m.setMemberStatic(2L);
                menberDao.save(m);
            }
        }
        //分页
        String jpal="from Member where 1=1";
        if(map1.get("hyname")!=null && !map1.get("hyname").equals("")){
            jpal=jpal+" and memberName like '%"+map1.get("hyname")+"%'";
        }
        int ktype=(int)map1.get("ktype");
        if(ktype!=0){
            jpal=jpal+"and membertypes="+ktype;
        }
        Query qu=entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((int)map1.get("qi"));
        //结束页数
        qu.setMaxResults((int)map1.get("shi"));

        //查询多少条数据
        String jpa="select count(m) from Member m where 1=1";

        if(map1.get("hyname")!=null && !map1.get("hyname").equals("")){
            jpa=jpa+" and memberName like '%"+map1.get("hyname")+"%'";
        }
        if(ktype!=0){
            jpa=jpa+" and membertypes="+ktype;
        }
        Long count=(Long) entityManager.createQuery(jpa).getSingleResult();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",count);
        map.put("rows",qu.getResultList());
        return map;
    }

    //会员充值
    public Map<String,Object> query2(Map<String,Object> map1){
        //分页
        String jpal="from Member where 1=1";
        if(map1.get("hyname")!=null && !map1.get("hyname").equals("")){
            jpal=jpal+" and memberName like '%"+map1.get("hyname")+"%'";
        }
        int ktype=(int)map1.get("ktype");
        if(ktype!=0){
            jpal=jpal+" and membertypes="+ktype;
        }
       // jpal=jpal+=" and memberStatic = 2";
        Query qu=entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((int)map1.get("qi"));
        //结束页数
        qu.setMaxResults((int)map1.get("shi"));

        //查询多少条数据
        String jpa="select count(m) from Member m where 1=1";

        if(map1.get("hyname")!=null && !map1.get("hyname").equals("")){
            jpa=jpa+" and memberName like '%"+map1.get("hyname")+"%'";
        }
        if(ktype!=0){
            jpa=jpa+" and membertypes="+ktype;
        }
        //jpa=jpa+=" and memberStatic = 2";
        Long count=(Long) entityManager.createQuery(jpa).getSingleResult();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",count);
        map.put("rows",qu.getResultList());
        return map;
    }


    //删除
   public int del(int id){
        Long memid=new Long(id);
        menberDao.deleteById(memid);
        return 1;
   }

//增加
  public int insert(Member member){
        menberDao.save(member);
       return  1;
   }
//修改
   public int update(Member member){
        menberDao.save(member);
        return 1;
   }
   public int upd(Member member){
        Member member1=menberDao.findById(member.getMemberId()).get();
        member1.setMemberbalance(member.getMemberbalance());
        menberDao.save(member1);
        return 1;
    }
}
